package com.thinksns.sociax.t4.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.thinksns.sociax.android.R;
import com.thinksns.sociax.t4.model.EventListModel;
import com.thinksns.sociax.thinksnsbase.utils.TimeHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhiyicx on 2017/4/26.
 */

public class EventListAdapter extends BaseAdapter {
    private Context context;
    private LayoutInflater inflater;
    private List<EventListModel.DataBean> datas;

    public EventListAdapter(Context context){
        this.context = context;
        inflater = LayoutInflater.from(context);
        datas = new ArrayList<>();
    }

    public void setDatas(List<EventListModel.DataBean> datas){
        this.datas.clear();
        this.datas.addAll(datas);
        notifyDataSetChanged();
    }

    public void insertDatas(List<EventListModel.DataBean> datas){
        this.datas.addAll(datas);
        notifyDataSetChanged();
    }

    public void clear(){
        this.datas.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return datas == null ? 0 : datas.size();
    }

    @Override
    public Object getItem(int i) {
        return datas.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        EventListModel.DataBean data = datas.get(i);
        if(view == null){
            viewHolder = new ViewHolder();
            view = inflater.inflate(R.layout.event_list_item,null);
            viewHolder.event_item_img = (ImageView) view.findViewById(R.id.event_item_img);
            viewHolder.event_item_name = (TextView) view.findViewById(R.id.event_item_name);
            viewHolder.event_item_time = (TextView) view.findViewById(R.id.event_item_time);
            viewHolder.event_item_place = (TextView) view.findViewById(R.id.event_item_place);
            viewHolder.event_item_number = (TextView) view.findViewById(R.id.event_item_number);
            viewHolder.event_item_money = (TextView) view.findViewById(R.id.event_item_money);

            Glide.with(context).load(data.getImage())
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .crossFade()
                    .into(viewHolder.event_item_img);
            viewHolder.event_item_name.setText(data.getName());
            viewHolder.event_item_time.setText(
                    TimeHelper.getStandardTimeUntilMinites(data.getStime() * 1000l ) + " 至 "
                    + TimeHelper.getStandardTimeUntilMinites(data.getEtime() * 1000l )
            );
            viewHolder.event_item_place.setText(data.getLocation());
            viewHolder.event_item_number.setText(data.getNum() + "人参与活动");
            viewHolder.event_item_money.setText("￥ " + data.getPrice());
            view.setTag(viewHolder);

        }else{
            viewHolder = (ViewHolder) view.getTag();
            Glide.with(context).load(data.getImage())
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .crossFade()
                    .into(viewHolder.event_item_img);
            viewHolder.event_item_name.setText(data.getName());
            viewHolder.event_item_time.setText(
                    TimeHelper.getStandardTimeUntilMinites(data.getStime() * 1000l) + " 至 "
                            + TimeHelper.getStandardTimeUntilMinites(data.getEtime() * 1000l)
            );
            viewHolder.event_item_place.setText(data.getLocation());
            viewHolder.event_item_number.setText(data.getNum() + "人参与活动");
            viewHolder.event_item_money.setText("￥ " + data.getPrice());
        }

        return view;
    }

    public class ViewHolder{
        ImageView event_item_img;
        TextView event_item_name;
        TextView event_item_time;
        TextView event_item_place;
        TextView event_item_number;
        TextView event_item_money;
    }
}
